package com.dodoke.ojserver.controller;

import com.dodoke.ojserver.bean.Solution;
import com.dodoke.ojserver.bean.SourceCodeUser;
import com.dodoke.ojserver.bean.dto.SolutionAndSourceDTO;
import com.dodoke.ojserver.bean.mapper.SolutionAndSourceMapStruct;
import com.dodoke.ojserver.config.Result;
import com.dodoke.ojserver.service.ISolutionAndSourceService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/solution")
@Api(tags = "用户代码接口")
public class SolutionAndSourceController {

    @Autowired
    private ISolutionAndSourceService iSolutionAndSourceService;

    @PostMapping("/solution")
    public Result addSolutionAndSourceCodeUser(@RequestBody SolutionAndSourceDTO solutionAndSourceDTO, HttpServletRequest request){
        Solution solution = SolutionAndSourceMapStruct.SOLUTION_AND_SOURCE_MAP_STRUCT.solutionAndSourceDTOToSolutionAndSource(solutionAndSourceDTO);
        // 设置问题创建时间
        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
        Timestamp time = Timestamp.valueOf(current);
        solution.setInDate(time);
        if (request.getHeader("x-forwarded-for") == null) {
            System.out.println(request.getRemoteAddr());
        }
        System.out.println(request.getHeader("x-forwarded-for"));
//        iSolutionAndSourceService.addSolution(solution);
        System.out.println(solution);
        System.out.println(solutionAndSourceDTO);
        return null;
    }

    @PostMapping("/source")
    public Result addSourceCodeUser(SourceCodeUser sourceCodeUser){
        iSolutionAndSourceService.addSourceCodeUser(sourceCodeUser);
        return null;
    }

}
